<?php


namespace App\Http\Controllers\Admin;
use App\Exports\UserExport;
use App\Models\User;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class UserController extends BaseController
{
    public function index(Request $request) {
        $title = $request->input('title');
        $audit_status = $request->input('audit_status', -1);
        $role = $request->input('role', 1);
        $is_black = $request->input('is_black');
        $is_punish = $request->input('is_punish', -1);
        $is_export = $request->input('is_export', false);
        $arr_role = explode(",", $role);

        if ($is_export) {
            $limit = 50000;
        } else {
            $limit = $request->get('per_page',20);
        }
        $users = User::whereIn('role', $arr_role)->when($title, function ($q) use($title) {
            $q->where('title', 'like', '%' . $title . '%');
        })->when($audit_status != -1, function ($q) use($audit_status) {
            $q->where('audit_status', $audit_status);
        })->when($is_black, function ($q) use($is_black) {
            $q->where('is_black', $is_black);
        })->when($is_punish != -1, function ($q) {
            $q->where(function ($qt) {
               $qt->where('not_sign_in_times', '>', 0)->orWhere('not_sign_out_times', '>', 0);
            });
        })->orderBy('id', 'desc')->paginate($limit);

        if ($is_export) {
            $res = $users->toArray();
            $title_map = [
                '1' => '1司机押运员',
                '2,3' => '单位管理'
            ];
            return Excel::download(new UserExport($res['data'], $role),  date('Y-m-d') . ".xlsx");
        }
        return $this->paginate($users);
    }

    public function info(Request $request) {
        $this->validate($request, [
            'id' => 'required|exists:users'
        ]);
        $id = \request("id");
        $user = User::find($id);
        return $this->success($user);
    }

    public function store(Request $request) {
        $this->validate($request, [
            'id' => 'required|exists:users'
        ]);
        $input = $request->input();
        $user = new User();
        $user->where('id', $input['id'])->update($input);
        $cache_key = $user->getMorphClass() . ":" . $input['id'];
        \Cache::forget($cache_key);
        return $this->success("修改成功", ['id' => $input['id']]);
    }

    public function destroy(Request $request) {
        $this->validate($request, [
            'id' => 'required|exists:users'
        ]);
        $id = $request->input('id');
        $user = new User();
        $user->where('id', $id)->delete();
        $cache_key = $user->getMorphClass() . ":" . $id;
        \Cache::forget($cache_key);
        return $this->success([]);
    }


    public function clearPunish() {
        User::chunk(100, function ($list) {
           foreach ($list as $item) {
               $item->not_sign_in_times = 0;
               $item->not_sign_out_times = 0;
               $item->save();
               $cache_key = $item->getMorphClass() . ":" . $item->id;
               \Cache::forget($cache_key);
           }
        });
        return $this->success([]);
    }

    public function updatePunish(Request $request) {
        $this->validate($request, [
            'id' => 'required',
            'not_sign_in_times' => 'required',
            'not_sign_out_times' => 'required',
        ]);
        $input = $request->input();
        $user = User::find($input['id']);
        if (!$user)
            return $this->failed('用户不存在');

        $user->not_sign_in_times = $input['not_sign_in_times'];
        $user->not_sign_out_times = $input['not_sign_out_times'];
        $user->save();
        $cache_key = $user->getMorphClass() . ":" . $user->id;
        \Cache::forget($cache_key);
        return $this->success([]);
    }

    public function updateNotice(Request $request) {
        $this->validate($request, [
            'id' => 'required',
            'notice' => 'required',
        ]);
        $input = $request->input();
        $user = User::find($input['id']);
        if (!$user)
            return $this->failed('用户不存在');

        $user->notice = $input['notice'];
        $user->save();
        $cache_key = $user->getMorphClass() . ":" . $user->id;
        \Cache::forget($cache_key);
        return $this->success([]);
    }


}

